<template>
	<view class="index">
		<view class="top">
			<image v-if="verifierimg" :src="httpimg+verifierimg" mode=""></image>
			<!-- <image src="../../static/logo.png" mode=""></image> -->
			<view class="top_bg">
				<view class="top_box">
					<view class="left">
						<view class="l_img">
							<image v-if="avatar" :src="http + avatar" mode=""></image>
						</view>
						<view class="l_text">
							{{name}}
						</view>
					</view>
					<view class="right">
						<view class="r_img">
							<image src="../../static/tuichu.png" mode=""></image>
						</view>
						<view class="r_text" @click="toQuit">
							退出
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="content">
			<view class="c_box purple" @click="scanQRCode">
				<view class="c_text">
					扫码核销
				</view>
				<view class="c_img">
					<image src="../../static/saomahexiao.png" mode=""></image>
				</view>
			</view>
			<navigator url="record" class="c_box blue">
				<view class="c_text">
					核销记录
				</view>
				<view class="c_img">
					<image src="../../static/hexiaojilu.png" mode=""></image>
				</view>
			</navigator>
		</view>
		<view class="input">
			<view class="i_left">
				<input type="text" v-model="code" placeholder="请输入核销编码" />
			</view>
			<view class="i_right" @click="toSubmit">
				提交
			</view>
		</view>
		<view class="index_bottom">
			<view class="title">
				最近核销记录
			</view>
			<view class="navlist">
				<view class="item" :class="active == index?'actives':''" v-for="(item,index) in typeList"
					@click="change(item,index)" :key="index">
					{{item.name}}
				</view>
			</view>
			<view class="b_list" v-for="(item,index) in list" :key="index" @click="toDetails(item)">
				<view class="l_left">
					<view class="img">
						<image :src="httpimg + item.ticket.image" mode=""></image>
					</view>
					<view class="text">
						<view class="">
							{{item.ticket.title}}
						</view>
						<view class="time">
							{{$formatDate(item.createtime*1000)}}
						</view>
					</view>
				</view>
				<view class="l_right">
					<view class="">
						已核销
					</view>
					<view class="num">
						x{{item.order.number}}
					</view>
				</view>
			</view>
			<kong v-if="list.length <= 0"></kong>
		</view>
		<view class="" style="height: 100rpx;">
			
		</view>
	</view>
</template>

<script>
	import wx from '@/common/jweixin-1.6.0.js'
	export default {
		data() {
			return {
				infoss:{},
				name: '',
				code: '', //1530807114408621803 2530807135830218624 3530807113808029503 4530807135941013676
				verifierimg: '',
				http: this.$http,
				httpimg: this.$httpimg,
				avatar: '',
				params: {
					mid: '',
					rand: Math.floor(Math.random() * 100),
					page: 1,
					limit: 10,
					ticketcate_id: 8
				},
				typeList: [],
				active: 0,
				list: [],
				params1:{
					mid: '',
				}
			}
		},
		onLoad() {
			if (uni.getStorageSync('user')) {
				this.params.mid = uni.getStorageSync('user').id
				this.params1.mid = uni.getStorageSync('user').id
				this.name = uni.getStorageSync('user').name
				this.avatar = uni.getStorageSync('user').avatar
				let rand = Math.floor(Math.random() * 100)
				this.$post('home/index', {
					mid: this.params.mid,
					rand: rand
				}).then(res => {
					if (res.code == 0) {
						this.verifierimg = res.data.verifierimg
					}
				})
				this.$post('home/cate', this.params1).then(res => {
					if (res.code == 0) {
						this.typeList = res.data
						this.getlist()
					}
				})
			}
		},
		onReady() {
			this.confirmJdk()
		},
		methods: {
			getlist() {
				this.$post('home/recordList', this.params).then(res => {
					if (res.code == 0) {
						this.list = res.data.list
					}
				})
			},
			change(item, index) {
				this.active = index
				this.params.ticketcate_id = item.id
				this.getlist()
			},
			toDetails(item) {
				console.log(item.order.orderno);
				uni.navigateTo({
					url: `details?code=${item.order.orderno}`
				})

			},
			toSubmit() {
				if (this.code == '') {
					uni.showToast({
						icon: 'none',
						title: '请输入核销编码'
					})
				} else {
					let rand = Math.floor(Math.random() * 100)
					this.$post('home/orderDetail', {
						code: this.code,
						mid: this.params.mid,
						rand: rand
					}).then(res => {
						if (res.code == 0) {
							uni.navigateTo({
								url: `order?code=${this.code}&type=${this.type}`
							})
							this.code = ''
						}
					})
				}

			},

			toQuit() {
				uni.showModal({
					title: '是否退出？',
					confirmColor: '#FF7416',
					success: (res) => {
						if (res.confirm) {
							uni.removeStorageSync('user')
							uni.reLaunch({
								url: 'login'
							})
						} else if (res.cancel) {
						}
					}
				});
			},
			confirmJdk() {
				let locationHref = location.href.split('#')[0];
				this.$post2('wechat/scancode', {
					url: locationHref
				}).then(data => {
					wx.config({
						appId: data.data.appId, // 和获取Ticke的必须一样------必填，公众号的唯一标识
						timestamp: data.data.timestamp, // 必填，生成签名的时间戳
						nonceStr: data.data.nonceStr, // 必填，生成签名的随机串
						signature: data.data.signature, // 必填，签名，见附录1
						jsApiList: ['scanQRCode']
					});
					wx.ready(() => {
						wx.checkJsApi({
							jsApiList: ['scanQRCode'], // 需要检测的JS接口列表，所有JS接口列表见附录2,
							success: (res) => {
								// window.location.href = data.data.url
								console.log('返回信息',data.data.url)
							}
						});
					})
				})
			},
			scanQRCode() {
				let that = this
				wx.scanQRCode({
					needResult: 1, // 默认为0，扫描结果由微信处理，1则直接返回扫描结果，
					scanType: ["qrCode"], // 可以指定扫二维码还是一维码，默认二者都有
					success: (resss) => {
						let rand = Math.floor(Math.random() * 100)
						this.$post('home/orderDetail', {
							code: resss.resultStr,
							mid: this.params.mid,
							rand: rand
						}).then(res => {
							if (res.code == 0) {
								uni.navigateTo({
									url: `order?code=${resss.resultStr}&type=${this.type}`
								})
							}
						})
					}
				});
			}
		}
	}
</script>

<style lang="scss">
	.index ::-webkit-scrollbar {
		display: none;
		width: 0 !important;
		height: 0 !important;
		-webkit-appearance: none;
		background: transparent;
	}

	image {
		width: 100%;
		height: 100%;
	}

	.index {
		.top {
			width: 100%;
			height: 360rpx;
			position: relative;

		}

		.top_bg {
			width: 100%;
			height: 86rpx;
			position: absolute;
			top: 0;
			left: 0;
			background-color: rgba(255, 255, 255, .6);

			.top_box {
				width: 100%;
				height: 100%;
				display: flex;
				padding: 0 33rpx;
				justify-content: space-between;
				align-items: center;
				box-sizing: border-box;

				.left {
					display: flex;
					align-items: center;

					.l_img {
						width: 64rpx;
						height: 64rpx;
						border-radius: 50%;
						overflow: hidden;
					}

					.l_text {
						font-size: 28rpx;
						font-weight: 500;
						color: #fff;
						margin-left: 17rpx;
					}
				}

				.right {
					display: flex;
					align-items: center;

					.r_img {
						width: 34rpx;
						height: 34rpx;
					}

					.r_text {
						font-size: 28rpx;
						font-weight: 500;
						color: #fff;
						margin-left: 10rpx;
					}
				}
			}
		}

		.content {
			width: 690rpx;
			box-sizing: border-box;
			display: flex;
			align-items: center;
			justify-content: space-around;
			margin: 40rpx auto;

			.c_box {
				width: 284rpx;
				height: 180rpx;
				opacity: 1;
				border-radius: 10rpx;
				padding-top: 40rpx;
				box-sizing: border-box;

				.c_text {
					text-align: center;
					font-size: 32rpx;
					font-weight: 700;
					color: #ffffff;
				}

				.c_img {
					width: 52rpx;
					height: 52rpx;
					border-radius: 50%;
					overflow: hidden;
					margin: 0 auto;
					margin-top: 17rpx;
				}
			}

			.purple {
				background: linear-gradient(180deg, #de9c5c 0%, #7264f9 0%, #a09bfe 100%);
			}

			.blue {
				background: linear-gradient(180deg, #de9c5c 0%, #009fd3 0%, #48c1e1 100%);
			}
		}

		.input {
			width: 690rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;
			box-sizing: border-box;
			margin: 0 auto;

			.i_left {
				width: 540rpx;
				height: 64rpx;
				opacity: 1;
				border: 1rpx solid #999999;
				border-radius: 33rpx;

				input {
					padding-left: 30rpx;
					box-sizing: border-box;
					width: 100%;
					height: 100%;
					font-size: 30rpx;
				}
			}

			.i_right {
				height: 64rpx;
				font-size: 30rpx;
				font-weight: 500;
				color: #fff;
				padding: 0rpx 30rpx;
				background-color: #FF7416;
				text-align: center;
				border-radius: 10rpx;
				line-height: 64rpx;
			}
		}

		.index_bottom {
			width: 690rpx;
			box-sizing: border-box;
			margin: 20rpx auto 0rpx;

			.title {
				font-size: 30rpx;
				font-weight: 700;
				color: #333333;
			}

			.navlist {
				width: 100%;
				height: 80rpx;
				display: flex;
				align-items: center;
				text-align: center;
				white-space: nowrap;
				overflow: hidden;
				overflow-x: auto;

				.item {
					display: inline-block;
					font-size: 30rpx;
					color: #000;
					padding: 0rpx 20rpx;
				}

				.actives {
					color: #FF7416;
					font-weight: bold;
					font-size: 32rpx;
				}
			}

			.b_list {
				display: flex;
				align-items: center;
				justify-content: space-between;
				padding-top: 20rpx;
				padding-bottom: 20rpx;
				box-sizing: border-box;
				border-bottom: 1rpx solid #f5f5f5;

				.l_left {
					display: flex;
					align-items: center;

					.img {
						width: 111rpx;
						height: 108rpx;
					}

					.text {
						font-size: 28rpx;
						font-weight: 700;
						color: #333333;
						margin-left: 20rpx;
						width: 440rpx;
						overflow: hidden;
						text-overflow: ellipsis;
						white-space: nowrap;

						.time {
							margin-top: 20rpx;
							font-size: 22rpx;
							font-weight: 500;
						}
					}
				}

				.l_right {
					text-align: right;
					font-weight: 500;
					color: #999999;
					font-size: 28rpx;

					.num {
						margin-top: 20rpx;
					}
				}
			}
		}
	}
</style>